/**
 * 
 */
package mx.frantor.catalogos.daoimp;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import mx.frantor.catalogos.dao.MaquinariaDAO;
import mx.frantor.catalogos.entitys.MaquinariaEntity;
import mx.frantor.util.HibernateUtil;

/**
 * @author Hp
 *
 */
public class MaquinariaDAOImpl implements MaquinariaDAO {

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.MaquinariaDAO#agregarMaquinaria(mx.frantor.catalogos.entitys.MaquinariaEntity)
	 */
	@Override
	public String agregarMaquinaria(MaquinariaEntity nuevoMaquinaria) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		
		session.beginTransaction();
		try {
		session.save(nuevoMaquinaria);
		session.flush();
		session.getTransaction().commit();
		e = nuevoMaquinaria.getIdMaquinaria().toString();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();

		System.out.println("agregar maquinaria resultado " + e);

		return e;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.MaquinariaDAO#modificarMaquinaria(mx.frantor.catalogos.entitys.MaquinariaEntity)
	 */
	@Override
	public String modificarMaquinaria(MaquinariaEntity modificarMaquinaria) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
	
		session.beginTransaction();
		try {
			MaquinariaEntity maquinariaTemp = (MaquinariaEntity) session.get(
					MaquinariaEntity.class, modificarMaquinaria.getIdMaquinaria());
		session.flush();
		session.evict(maquinariaTemp);
		session.update(modificarMaquinaria);
		session.getTransaction().commit();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();

		System.out.println("modificar maquinaria resultado " + e);

		return e;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.MaquinariaDAO#eliminarMaquinaria(java.lang.Integer)
	 */
	@Override
	public String eliminarMaquinaria(long idMaquinaria) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		

			// TODO Auto-generated method stub			
			session.beginTransaction();
			try {
				MaquinariaEntity MaquinariaElimina = (MaquinariaEntity) session.get(
						MaquinariaEntity.class, idMaquinaria);
			session.delete(MaquinariaElimina);
			session.getTransaction().commit();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		//HibernateUtil.getSessionFactory().getCurrentSession().close();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("eliminar maquinaria resultado " + e);

		return e;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.MaquinariaDAO#getTodosMaquinariaActivos()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MaquinariaEntity> getTodosMaquinariaActivos() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<MaquinariaEntity> maquinaria = session.createCriteria(MaquinariaEntity.class)
				.addOrder(Order.asc("IdMaquinaria")).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return maquinaria;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.MaquinariaDAO#getMaquinariaId(java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public MaquinariaEntity getMaquinariaId(long idMaquinaria) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<MaquinariaEntity> maquinaria = (List<MaquinariaEntity>) session.createCriteria(MaquinariaEntity.class)
				.add(Restrictions.eq("IdMaquinaria", idMaquinaria))
				.setFetchSize(1).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		if(maquinaria==null){
			return null;
		}else{
			return maquinaria.get(0);
		}
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.MaquinariaDAO#getMaquinariaPorId(java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MaquinariaEntity> getMaquinariaPorId(long idMaquinaria) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<MaquinariaEntity> maquinaria = (List<MaquinariaEntity>) session.createCriteria(MaquinariaEntity.class)
				.add(Restrictions.eq("IdMaquinaria", idMaquinaria))
				.list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return maquinaria;
	}

}
